//
//  TieBaXiangQingViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/14.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//*********************贴吧详情*********************
#import "TieBaXiangQingViewController.h"
#import "UMSocial.h"
@interface TieBaXiangQingViewController ()<TieBaXiangqingTableViewDelegate,UITextViewDelegate,UMSocialUIDelegate>
@property(nonatomic,copy)UIView *footerView;
@property(nonatomic,copy)UIButton *startBtn;
@property(nonatomic,copy)UIButton *cancelBtn;
@property(nonatomic,copy)TieBaXiangQingTableView *myTable;
//@property(nonatomic,copy)UITextField *answerTextField;
@property(nonatomic,copy)NSString *commentName;
/*
 *评论
 */
@property(nonatomic,copy)UITextView *commentTextView;
/**
 *编辑
 */
@property(nonatomic,copy)UIView *editView;
/**
 *隐藏editView
 */
@property(nonatomic,assign)BOOL editViewIsShow;




/*
 *帖子标题 内容
 */
@property(nonatomic,copy)NSString *topic_title;
@property(nonatomic,copy)NSString *topic_content;

@property(nonatomic,copy)NSString *replyId;



@property(nonatomic,copy)NSString *shareTitle;

@property(nonatomic,copy)NSString *shareContent;

@property(nonatomic,copy)UIImage *shareImage;

@end

@implementation TieBaXiangQingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[UIView new]];
    self.view.backgroundColor=[UIColor whiteColor];
    _editViewIsShow=false;
    [self createNavigation];
   // NSLog(@"==============%@",_arr);
    [self createUI];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveTopicData:) name:@"ShareContent" object:nil];
    // Do any additional setup after loading the view.
}
-(void)receiveTopicData:(NSNotification *)notify
{
    NSDictionary *dic=notify.userInfo;
    _topic_title=dic[@"title"];
    _topic_content=dic[@"content"];
}


-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //标题
    UILabel *biaoti=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    biaoti.text=@"帖子详情";
    biaoti.textColor=[UIColor whiteColor];
    biaoti.font=[UIFont systemFontOfSize:22];
    biaoti.textAlignment=NSTextAlignmentCenter;
    self.navigationItem.titleView=biaoti;
    
    //返回
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(0, 0, 25, 25);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
    //编辑
    //分享
    UIButton *shareBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame=CGRectMake(0, 0, 30, 30);
    shareBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    //    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    //    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [shareBtn setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //收藏
    UIButton *collectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    collectBtn.frame=CGRectMake(0, 0,30, 30);
    collectBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    //    [collectBtn setTitle:@"收藏" forState:UIControlStateNormal];
    //    [collectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [collectBtn setBackgroundImage:[UIImage imageNamed:@"collection"] forState:UIControlStateNormal];
    [collectBtn addTarget:self action:@selector(collectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItems=@[[[UIBarButtonItem alloc]initWithCustomView:shareBtn],[[UIBarButtonItem alloc]initWithCustomView:collectBtn]];
}
#pragma mark 编辑
-(void)rightBtnClick:(UIButton *)sender
{
    if (_editViewIsShow) {
        _editView.hidden=YES;
        _editViewIsShow=false;
    }else{
        _editView.hidden=NO;
        _editViewIsShow=true;
    }
}
#pragma mark 返回
-(void)backBtnClick:(UIButton *)sender
{
    if (_backState==1) {
        self.navigationController.navigationBar.hidden=YES;
    }else{
         self.navigationController.navigationBar.hidden=NO;
    }
    if (_barState==1) {
        self.tabBarController.tabBar.hidden=NO;
    }else{
        self.tabBarController.tabBar.hidden=YES;
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)initData
{
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSArray *arr=[user objectForKey:@"myArr"];
    if (arr==nil) {
        //[user setObject:_arr forKey:@"myArr"];
    }
    
}

-(void)createUI
{
    TieBaXiangQingTableView *table=[[TieBaXiangQingTableView alloc]initWithFrame:CGRectMake(0, 20+44, self.view.frame.size.width, self.view.frame.size.height-20-44-60) style:UITableViewStyleGrouped TopicId:_topic_id];
    table.centerTag=_centerTag;
    table.imgDelegate=self;
    table.fromWhere=_fromWhere;
    [self.view addSubview:table];
    [self.view addSubview:[self createTableFooterView]];
    _myTable=table;
    table.backTopicData=^(NSString *title,NSString *content, UIImage *image){
        _shareTitle=title;
        _shareContent=content;
        _shareImage=image;
    };
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardIsShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardIsHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    
    
//    //编辑
//    _editView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-10-30-20-30-20-1, 20+44, 30+20+30+20+5, 60)];
//    UIImageView *editViewbackgroundImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _editView.frame.size.width,_editView.frame.size.height)];
//    editViewbackgroundImageView.image=[UIImage imageNamed:@"shequ_editKuang"];
//    editViewbackgroundImageView.contentMode=UIViewContentModeScaleAspectFit;
//    [_editView addSubview:editViewbackgroundImageView];
    
}
#pragma mark 分享
-(void)shareBtnClick:(UIButton *)sender
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:[NSString stringWithFormat:@"#%@#%@",_shareTitle,_shareContent]
                                     shareImage:_shareImage
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina,UMShareToTencent,UMShareToQzone,UMShareToQQ,UMShareToRenren,UMShareToDouban,nil]
                                       delegate:self];
    
}

-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
        [self.view showTiShiMessage:@"分享成功"];
    }
}

#pragma mark 收藏
-(void)collectBtnClick:(UIButton *)sender
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    // [self isLoading:YES];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager POST:[NSString stringWithFormat:CollectionTopic,token,_topic_id] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view showTiShiMessage:@"收藏成功"];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"collectionData===============%@",dic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view showTiShiMessage:@"收藏失败"];
        NSLog(@"%@",error.localizedDescription);
    }];

}



#pragma mark 显示键盘
-(void)keyBoardIsShow:(NSNotification *)notify
{
    _startBtn.hidden=NO;
    _cancelBtn.hidden=NO;
    NSDictionary *dic=notify.userInfo;
   // NSLog(@"=============%@",dic);
    CGRect frame=[[dic objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    _footerView.frame=CGRectMake(0, frame.origin.y-90, self.view.frame.size.width, 90);
}
#pragma mark 隐藏键盘
-(void)keyBoardIsHidden:(NSNotification *)notify
{
    _startBtn.hidden=YES;
    _cancelBtn.hidden=YES;
    NSDictionary *dic=notify.userInfo;
   // NSLog(@"**************%@",dic);
    CGRect frame=[[dic objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    _footerView.frame=CGRectMake(0, frame.origin.y-60, self.view.frame.size.width, 70);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//TieBaTableView的代理
-(void)bigImage:(NSInteger)currentIndex AndImageArrays:(NSArray *)imageArrays
{
    CommunityBigImageViewController *vc=[[CommunityBigImageViewController alloc]init];
    // vc.image=img;
    vc.navigationBarState=1;
    vc.imageArrays=[NSArray arrayWithArray:imageArrays];
    vc.currentIndex=currentIndex;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark tableFooterView
-(UIView *)createTableFooterView
{
    UIView *footerView=[[UIView alloc]init];
    footerView.frame=CGRectMake(0, self.view.frame.size.height-60, self.view.frame.size.width, 70);
    footerView.backgroundColor=[UIColor colorWithRed:210/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    
    //评论
    _commentTextView=[[UITextView alloc]initWithFrame:CGRectMake(15, 5, self.view.frame.size.width-15*2, 50)];
    _commentTextView.textColor=[UIColor grayColor];
    _commentTextView.text=@"评论";
    _commentTextView.delegate=self;
    _commentTextView.font=[UIFont systemFontOfSize:15];
    _commentTextView.backgroundColor=[UIColor whiteColor];
    _commentTextView.layer.borderColor=[UIColor blackColor].CGColor;
    _commentTextView.layer.borderWidth=1.0;
    _commentTextView.layer.cornerRadius=2.0;
    [footerView addSubview:_commentTextView];
    
    
    
    
    
    
    //取消
    UIButton *cancelBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.hidden=YES;
    cancelBtn.frame=CGRectMake(10, 55, 40, 30);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font=[UIFont systemFontOfSize:18];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelComment) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //发送
    UIButton *startBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    startBtn.hidden=YES;
    startBtn.frame=CGRectMake(footerView.frame.size.width-10-40, 55, 40, 30);
    [startBtn setTitle:@"发送" forState:UIControlStateNormal];
    startBtn.titleLabel.font=[UIFont systemFontOfSize:18];
    [startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startComment
                                              ) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:startBtn];
    
    [footerView addSubview:cancelBtn];
    
    _startBtn=startBtn;
    _cancelBtn=cancelBtn;
    
    
     _footerView=footerView;
    return footerView;
}

//取消评论
-(void)cancelComment
{
   // _answerTextField.placeholder=@"评论";
    _commentTextView.text=@"评论";
    [self.navigationController.view endEditing:YES];
}
//发表评论
-(void)startComment
{
    NSLog(@"发表评论");
    
    
    //_commentTextView.text=@"评论";
   // _answerTextField.placeholder=@"评论";
    if (_commentTextView.text.length==0) {
        [self.view showTiShiMessage:@"内容不能为空"];
    }else{
        if (_commentName==nil) {
            _myTable.sendComment(_commentTextView.text,_replyId);
        }else{
            _myTable.sendComment([NSString stringWithFormat:@"@%@:%@",_commentName,_commentTextView.text],_replyId);
        }
    }
    [self.navigationController.view endEditing:YES];
    //_answerTextField.text=@"";
    _commentTextView.text=@"评论";
    _commentName=nil;
}

-(void)startPostComment:(NSString *)name ReplyId:(NSString *)replyId
{
    _replyId=replyId;
    _commentName=name;
    //_answerTextField.placeholder=
    _commentTextView.text=[NSString stringWithFormat:@"@%@:",name];
    [_commentTextView becomeFirstResponder];
   // [_answerTextField becomeFirstResponder];
}

//UITextView的代理
-(void)textViewDidBeginEditing:(UITextView *)textView
{
//    if ([textView.text isEqualToString:@"评论"]) {
           textView.text=@"";
//    }
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    textView.text=@"评论";
}

//点击用户头像
-(void)gotoMyAttentionViewController:(NSString *)userId
{
    NSLog(@"用户的id======%@",userId);
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSInteger currentUserId=[[user objectForKey:@"userId"] integerValue];
    
    NSLog(@"当前用户的id====%ld",currentUserId);
    NSInteger httpUserId=[userId integerValue];
    
    if (currentUserId == httpUserId) {
        //当前用户
        SelfViewController *vc=[[SelfViewController alloc]init];
        self.tabBarController.tabBar.hidden=YES;
        vc.userId=[userId integerValue];
        vc.barState=1;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        //我关注的 别人的
        MyAttentionViewController *vc=[[MyAttentionViewController alloc]init];
        vc.barState=1;
        vc.userId=userId;
        [self.navigationController pushViewController:vc animated:YES];
    }
   
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=NO;
    self.tabBarController.tabBar.hidden=YES;
    [self.navigationController.navigationBar setTranslucent:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
